#!/usr/bin/python3
# -*- coding: UTF-8 -*- 
# 设置utf-8  显示中文
"""
@Author: guo
@File：conftest.py
"""

import pytest
import requests
import sys

sys.path.append("../")
from common.cookie.get_web_cookie import GetWebCookie
from common.cookie.get_app_cookie_header import GetAppCookieHeader
from common.util.generate_device_key import GenerateDeviceKey
from config.get_conf_data import GetConfData
from common.yml.get_yml_keys import GetYmlKeys
from common.util.get_file_path import GetFilePath
from common.util.handle_jsonfile import HandleJsonFile

get_conf_data = GetConfData()
ymlkeys = GetYmlKeys()
fpath = GetFilePath()
hjson = HandleJsonFile()
web_login_data: dict = get_conf_data.get_web_login_request_data()
app_login_data: dict = get_conf_data.get_app_login_request_data()
logindata_key = ymlkeys.get_login_data_key()
res_filepath_key = ymlkeys.get_response_filepath_key()
token_key = ymlkeys.get_token_filepath_key()




@pytest.fixture(scope="class")
def web_login(request):
    login_data = request.param[logindata_key]
    res_filepath = request.param[res_filepath_key]
    token_path = request.param[token_key]

    web_login_data['username'] = login_data['username']
    web_login_data['password'] = login_data['password']
    res = requests.request(**web_login_data)
    get_cookies = GetWebCookie(res)

    # 当response_filepath 不为None时,则保存当前的登录响应结果
    if res_filepath !=None:
        res_filepath =fpath.get_file_abspath_json(res_filepath)
        hjson.write_dict_to_json_file(res_filepath,res.json())
    # 当token_filepath 不为None时,则保存当前的cookie
    if token_path !=None:
        token_path = fpath.get_file_abspath_json(token_path)
        token = get_cookies.get_cookie()
        hjson.write_dict_to_json_file(token_path,token)

    # return get_cookies


@pytest.fixture(scope="class")
def app_login(request):
    login_data = request.param[logindata_key]
    res_filepath = request.param[res_filepath_key]
    token_path = request.param[token_key]

    loginid = login_data['loginid']
    password = login_data['password']
    app_login_data['loginId'] = loginid
    app_login_data['password'] = password
    app_login_data['device'] = GenerateDeviceKey().generate_key_by_account(loginid)
    res = requests.request(**app_login_data)
    get_header = GetAppCookieHeader(res)

    # 当response_filepath 不为None时,则保存当前的登录响应结果
    if res_filepath != None:
        res_filepath = fpath.get_file_abspath_json(res_filepath)
        hjson.write_dict_to_json_file(res_filepath, res.json())
    # 当token_filepath 不为None时,则保存当前的cookie
    if token_path != None:
        token_path = fpath.get_file_abspath_json(token_path)
        token = get_header.get_header()
        hjson.write_dict_to_json_file(token_path, token)

    # return get_header


@pytest.fixture(scope="class")
def get_web_login_response(request):
    login_data = request.param[logindata_key]
    res_filepath = request.param[res_filepath_key]
    token_path = request.param[token_key]

    web_login_data['username'] = login_data['username']
    web_login_data['password'] = login_data['password']
    res = requests.request(**web_login_data)

    # 当response_filepath 不为None时,则保存当前的登录响应结果
    if res_filepath != None:
        res_filepath = fpath.get_file_abspath_json(res_filepath)
        hjson.write_dict_to_json_file(res_filepath, res.json())
    # 当token_filepath 不为None时,则保存当前的cookie
    if token_path != None:
        token_path = fpath.get_file_abspath_json(token_path)
        get_cookies = GetWebCookie(res)
        token = get_cookies.get_cookie()
        hjson.write_dict_to_json_file(token_path, token)

    # return res


@pytest.fixture(scope="class")
def get_app_login_response(request):
    login_data = request.param[logindata_key]
    res_filepath = request.param[res_filepath_key]
    token_path = request.param[token_key]

    loginid = login_data['loginid']
    password = login_data['password']
    app_login_data['loginId'] = loginid
    app_login_data['password'] = password
    app_login_data['device'] = GenerateDeviceKey().generate_key_by_account(loginid)
    res = requests.request(**app_login_data)

    # 当response_filepath 不为None时,则保存当前的登录响应结果
    if res_filepath != None:
        res_filepath = fpath.get_file_abspath_json(res_filepath)
        hjson.write_dict_to_json_file(res_filepath, res.json())
    # 当token_filepath 不为None时,则保存当前的cookie
    if token_path != None:
        token_path = fpath.get_file_abspath_json(token_path)
        get_header = GetAppCookieHeader(res)
        token = get_header.get_header()
        hjson.write_dict_to_json_file(token_path, token)

    # return res
